package Student;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;

/**
 * 显示学生详细信息、 更新学生信息。
 * @author Administrator
 *
 */

public class DetailStudent extends JDialog {

    protected JTextField jno = new JTextField(15);
    protected JTextField jname = new JTextField(15);
    protected JComboBox jprofession = new JComboBox(new String []{"","软件工程","物联网工程","计算机科学与技术","计算机应用技术","计算机网络技术"});
    JRadioButton jgender_man = new JRadioButton("男");
    JRadioButton jgender_woman = new JRadioButton("女", true);
    protected JTextField jage = new JTextField(15);
    protected JCheckBox jbox_sing = new JCheckBox("唱歌");
    protected JCheckBox jbox_dancing = new JCheckBox("跳舞");
    protected JCheckBox jbox_drawing = new JCheckBox("绘画");
    protected JTextField jtelephone = new JTextField(15);
    protected JTextField jqq = new JTextField(15);
    protected JTextField jaddress = new JTextField(15);

    protected JTextField jmath = new JTextField(14);
    protected JTextField jenglish = new JTextField(14);
    protected JTextField jjava = new JTextField(14);
    protected JTextField jdatastruct = new JTextField(14);
    protected JTextField jdatabase = new JTextField(14);
    protected JTextField jnet = new JTextField(14);
    protected JTextField jalgorithm = new JTextField(14);
    protected JTextField jos = new JTextField(14);
    protected JTextField jsumScore = new JTextField(14);

    JButton jbtn_save = new JButton("保存");
    JButton jbtn_reset = new JButton("重置");
    JButton jbtn_cancel = new JButton("取消");

    public DetailStudent(Student stu){
        this();
        this.setTitle("学生的详细信息");
        jbtn_save.setText("更新学生信息");
        jbtn_reset.setVisible(false);
        jbtn_cancel.setText("返回");
        setDefalutValueBy(stu);
        setJComponentsUnable();//


    }

    public DetailStudent() {
        this.setSize(580, 450);
        // 获取屏幕的尺寸
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        // 设置程序窗口的位置为屏幕的正中央
        this.setLocation((dimension.width-getWidth())/2, (dimension.height-getHeight())/2);
        this.setTitle("添加学生信息");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel jp1_center = new JPanel(new GridLayout(1, 2));
        JPanel jp1_south = new JPanel();
        this.add(jp1_center,BorderLayout.CENTER);
        this.add(jp1_south,BorderLayout.SOUTH);


        JPanel jp2_left = new JPanel();
        Border etched = BorderFactory.createEtchedBorder();
        Border border = BorderFactory.createTitledBorder(etched, "基本信息");
        jp2_left.setBorder(border); // 设置边框
        jp2_left.setLayout(new FlowLayout(FlowLayout.LEFT));

        JPanel jp2_right = new JPanel();
        border = BorderFactory.createTitledBorder(etched,"考试成绩");
        jp2_right.setBorder(border);
        jp2_right.setLayout(new GridLayout(9,1));

        jp1_center.add(jp2_left);
        jp1_center.add(jp2_right);

        JPanel temp=new JPanel();
        temp.add(new JLabel("学号："));
        temp.add(jno);
        jp2_left.add(temp);

        temp=new JPanel();
        temp.add(new JLabel("姓名："));
        temp.add(jname);
        jp2_left.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.LEFT));
        temp.add(new JLabel("性别："));
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(jgender_man);
        btnGroup.add(jgender_woman);
        temp.add(jgender_man);
        temp.add(jgender_woman);
        jp2_left.add(temp);

        temp=new JPanel();
        temp.add(new JLabel("专业："));
        temp.add(jprofession);
        jp2_left.add(temp);


        temp=new JPanel();
        temp.add(new JLabel("年龄："));
        temp.add(jage);
        jp2_left.add(temp);

        temp=new JPanel();
        temp.add(new JLabel("爱好："));
        temp.add(jbox_sing);
        temp.add(jbox_dancing);
        temp.add(jbox_drawing);
        jp2_left.add(temp);


        temp=new JPanel();
        temp.add(new JLabel("电话："));
        temp.add(jtelephone);
        jp2_left.add(temp);

        temp=new JPanel();
        JLabel jlab_qq = new JLabel("QQ：");
        jlab_qq.setPreferredSize(new Dimension(38,17));
        jlab_qq.setSize(new Dimension(38,17));
        temp.add(jlab_qq);
        temp.add(jqq);
        jp2_left.add(temp);

        temp=new JPanel();
        temp.add(new JLabel("住址："));
        temp.add(jaddress);
        jp2_left.add(temp);


        FocusListener focListener = new FocusAdapter() {
            public void focusLost(FocusEvent e) {
                JTextField jtext = (JTextField)e.getSource();
                int value  = new Integer(jtext.getText().equals("")?"0":jtext.getText());
                int sum = Integer.parseInt(jsumScore.getText().equals("")?"0":jsumScore.getText());
                jsumScore.setText(String.valueOf(sum + value));
            }
        };
        jmath.addFocusListener(focListener);
        jenglish.addFocusListener(focListener);
        jjava.addFocusListener(focListener);
        jdatastruct.addFocusListener(focListener);
        jdatabase.addFocusListener(focListener);
        jnet.addFocusListener(focListener);
        jalgorithm.addFocusListener(focListener);
        jos.addFocusListener(focListener);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("高等数学："));
        temp.add(jmath);
        temp.setAlignmentY(RIGHT_ALIGNMENT);
        jp2_right.add(temp);

        temp = new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("大学英语："));
        temp.add(jenglish);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("Java语言："));
        temp.add(jjava);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("数据结构："));
        temp.add(jdatastruct);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("数 据 库："));
        temp.add(jdatabase);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("计算机网络："));
        temp.add(jnet);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("算法分析："));
        temp.add(jalgorithm);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("操作系统："));
        temp.add(jos);
        jp2_right.add(temp);

        temp=new JPanel();
        temp.setLayout(new FlowLayout(FlowLayout.RIGHT));
        temp.add(new JLabel("总成绩："));
        jsumScore.setEnabled(false);
        temp.add(jsumScore);

        jp2_right.add(temp);


        jbtn_save.addActionListener(listener);
        jbtn_reset.addActionListener(listener);
        jbtn_cancel.addActionListener(listener);
        jp1_south.add(jbtn_save);
        jp1_south.add(jbtn_reset);
        jp1_south.add(jbtn_cancel);
    }

    private void setDefalutValueBy(Student stu){
        jno.setText(stu.getNo()+"");
        jname.setText(stu.getName());
        jprofession.setSelectedItem(stu.getProfession());
        jgender_man.setSelected(stu.getGender().equals("男"));
        jgender_woman.setSelected(stu.getGender().equals("女"));
        jage.setText(stu.getAge()==-1?"":String.valueOf(stu.getAge()));
//		jinterest.setText(stu.getInterest());
        jbox_sing.setSelected(stu.getInterest().contains("唱歌"));
        jbox_dancing.setSelected(stu.getInterest().contains("跳舞"));
        jbox_drawing.setSelected(stu.getInterest().contains("绘画"));
        jtelephone.setText(stu.getTelephone());
        jqq.setText(stu.getQq());
        jaddress.setText(stu.getAddress());

        jmath.setText(stu.getMath()==-1?"":String.valueOf(stu.getMath()));
        jenglish.setText(stu.getEnglish()==-1?"":String.valueOf(stu.getEnglish()));
        jjava.setText(stu.getJava()==-1?"":String.valueOf(stu.getJava()));
        jdatastruct.setText(stu.getDatastruct()==-1?"":String.valueOf(stu.getDatastruct()));
        jdatabase.setText(stu.getDatabase()==-1?"":String.valueOf(stu.getDatabase()));
        jnet.setText(stu.getNet()==-1?"":String.valueOf(stu.getNet()));
        jalgorithm.setText(stu.getAlgorithm()==-1?"":String.valueOf(stu.getAlgorithm()));
        jos.setText(stu.getOs()==-1?"":String.valueOf(stu.getOs()));
        jsumScore.setText(String.valueOf(stu.getSumScore()));
    }
    private void setJComponentsUnable(){
        jno.setEnabled(false);
        jname.setEnabled(false);
        jprofession.setEnabled(false);
        jgender_man.setEnabled(false);
        jgender_woman.setEnabled(false);
        jage.setEnabled(false);
//		jinterest.setEnabled(false);
        jbox_sing.setEnabled(false);
        jbox_dancing.setEnabled(false);
        jbox_drawing.setEnabled(false);
        jtelephone.setEnabled(false);
        jqq.setEnabled(false);
        jaddress.setEnabled(false);

        jmath.setEnabled(false);
        jenglish.setEnabled(false);
        jjava.setEnabled(false);
        jdatastruct.setEnabled(false);
        jdatabase.setEnabled(false);
        jnet.setEnabled(false);
        jalgorithm.setEnabled(false);
        jos.setEnabled(false);
//		jsumScore.setEnabled(false);
    }
    private void setJComponentsEnable(){
//		jno.setEnabled(true);
        jname.setEnabled(true);
        jprofession.setEnabled(true);
//		jgender.setEnabled(true);
        jgender_man.setEnabled(true);
        jgender_woman.setEnabled(true);
        jage.setEnabled(true);
//		jinterest.setEnabled(true);
        jbox_sing.setEnabled(true);
        jbox_dancing.setEnabled(true);
        jbox_drawing.setEnabled(true);
        jtelephone.setEnabled(true);
        jqq.setEnabled(true);
        jaddress.setEnabled(true);

        jmath.setEnabled(true);
        jenglish.setEnabled(true);
        jjava.setEnabled(true);
        jdatastruct.setEnabled(true);
        jdatabase.setEnabled(true);
        jnet.setEnabled(true);
        jalgorithm.setEnabled(true);
        jos.setEnabled(true);
    }

    /**
     * 内部类，用来处理产生的事件
     */
    class OKAndResetListener implements ActionListener {
        //成员变量
        DetailStudent detailInfoFrame;

        @Override
        public void actionPerformed(ActionEvent e) {
            String command = e.getActionCommand();
            if(command.equals("保存")){
                Student st = transferInforToStudent();
                if(Dao.getInstance().noHasExist(st.getNo())){
                    JOptionPane.showMessageDialog(null, "输入的学号已存在，请重新输入");
                    return;
                }
                Dao.getInstance().addRecord(st);
                if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(null, "信息已保存，是否继续添加？","是否继续输入？",JOptionPane.YES_NO_OPTION)){
                    reshowJFrame();
                }else{
                    closeJFrame();
                }
            }else if(command.equals("重置")){
                resetValues();
            }else if(command.equals("取消") || command.equals("返回")){// "取消"
                closeJFrame();
            }else if(command.equals("更新学生信息")){
                setJComponentsEnable();
                jbtn_save.setText("保存更新");
                jbtn_cancel.setText("取消");
            }else if(command.equals("保存更新")){
                Student st = transferInforToStudent();
                Dao.getInstance().updateInfoOf(st);
                JOptionPane.showMessageDialog(null, "信息已更新");
                closeJFrame();
            }

        }
        /**
         * 父类控件
         */
        public OKAndResetListener(DetailStudent parent){
            detailInfoFrame = parent;
        }
        // 其他方法
        /**
         * 将所有值置空
         */
        private void resetValues(){
            jno.setText("");
            jname.setText("");
            jprofession.setSelectedIndex(0);
            jage.setText("");
//			jinterest.setText("");
            jbox_sing.setSelected(false);
            jbox_dancing.setSelected(false);
            jbox_drawing.setSelected(false);

            jtelephone.setText("");
            jqq.setText("");
            jaddress.setText("");

            jmath.setText("");
            jenglish.setText("");
            jjava.setText("");
            jdatastruct.setText("");
            jdatabase.setText("");
            jnet.setText("");
            jalgorithm.setText("");
            jos.setText("");
            jsumScore.setText("");
        }
        /**
         * 将控件中的值赋值给Student类的对象
         */
        private Student transferInforToStudent(){
            Student st = new Student();
            st.setNo(new Integer(jno.getText()));
            st.setName(jname.getText());
            st.setProfession(jprofession.getSelectedItem().toString());
            st.setGender(jgender_man.isSelected()?"男":"女");
            st.setAge(jage.getText().isEmpty()?-1:new Integer(jage.getText()));
            StringBuffer interests = new StringBuffer();
            interests.append(jbox_sing.isSelected()?"唱歌":"")
                    .append(jbox_dancing.isSelected()?"跳舞":"")
                    .append(jbox_drawing.isSelected()?"绘画":"");
            st.setInterest(interests.toString());
            st.setTelephone(jtelephone.getText());
            st.setQq(jqq.getText());
            st.setAddress(jaddress.getText());

            st.setMath(jmath.getText().isEmpty()?-1:new Integer(jmath.getText()));
            st.setEnglish(jenglish.getText().isEmpty()?-1:new Integer(jenglish.getText()));
            st.setJava(jjava.getText().isEmpty()?-1:new Integer(jjava.getText()));
            st.setDatastruct(jdatastruct.getText().isEmpty()?-1:new Integer(jdatastruct.getText()));
            st.setDatabase(jdatabase.getText().isEmpty()?-1:new Integer(jdatabase.getText()));
            st.setNet(jnet.getText().isEmpty()?-1:new Integer(jnet.getText()));
            st.setAlgorithm(jalgorithm.getText().isEmpty()?-1:new Integer(jalgorithm.getText()));
            st.setOs(jos.getText().isEmpty()?-1:new Integer(jos.getText()));
//			st.setSoftware(jsumScore.getText().isEmpty()?-1:new Integer(jsumScore.getText()));
            return st;
        }

        /**
         * 将DetailInfo控件关闭
         * */
        private void closeJFrame(){
            detailInfoFrame.dispose();
//			或者直接用：
//			System.exit(0)

        }
        /**
         * 将DetailInfo控件重组
         * */
        private void reshowJFrame(){
            resetValues();
            detailInfoFrame.dispose();
            detailInfoFrame.setVisible(true);
        }
    }
    /** 监听器对象 */
    OKAndResetListener listener = new OKAndResetListener(this);



    public static void main(String[] args) {
        DetailStudent di = new DetailStudent();
        di.setVisible(true);
    }


}
